/* 
 * 
 * TabVD is a Virtual Desktop Client/Server designed specifically for connecting 
 *  from Tablet like devices.
 *  
 *  Copyright (C) 2011 Subhabrata Ghosh 
 *  
 *  This program is free software: you can redistribute it and/or modify it under 
 *  the terms of the GNU General Public License as published by the Free Software 
 *  Foundation, either version 3 of the License, or (at your option) any later 
 *  version. 
 *  
 *  This program is distributed in the hope that it will be useful, but WITHOUT 
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 *  details. 
 *  
 *  You should have received a copy of the GNU General Public License along with 
 *  this program. If not, see <http://www.gnu.org/licenses/>. 
 * 
 * 
 */
#ifndef FILEUTILS_H_
#define FILEUTILS_H_

#include <boost/filesystem/operations.hpp>
#include "CommonDefs.H"

namespace tabvd {
    namespace fs = boost::filesystem;
    class FileUtils {
        public:
            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Check if the specified path exists and is a normal file.
            *
            * @Param filename
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool existsf(const std::string filename) {
                fs::path fp(filename);
                if (!fs::exists(fp))
                    return false;
                if (!fs::is_regular(fp))
                    return false;
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Check if the specified path exists and is a directory.
            *
            * @Param directory
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool existsd(const std::string directory) {
                fs::path fp(directory);
                if (!fs::exists(fp))
                    return false;
                if (!fs::is_directory(fp))
                    return false;
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Get the size of the specified file.
            *
            * @Param filename
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static int sizef(const std::string filename) {
                fs::path fp(filename);
                if (!fs::exists(fp)) {
                    throw Exception("File not found...");
                }

                if (!fs::is_regular(fp))
                    return -1;
                return fs::file_size(fp);
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Create a new directory, if path exists and is a directory 
            *  will return true.
            *
            * @Param directory
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool mkdir(const std::string directory) {
                fs::path fp(directory);
                if (fs::exists(fp)) {
                    if (fs::is_directory(fp))
                        return true;
                    throw Exception("Path already exists and is not a directory.");
                }
                return fs::create_directory(fp);
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Make directories recursively.
            *
            * @Param directory
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool mkdirp(const std::string directory) {
                fs::path fp(directory);
                if (fs::exists(fp)) {
                    if (fs::is_directory(fp))
                        return true;
                    throw Exception("Path already exists and is not a directory.");
                }
                return fs::create_directories(fp);
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Remove the specified path (file or directory).
            *
            * @Param path
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool remove(const std::string path) {
                fs::path fp(path);
                if (!fs::exists(fp))
                    return false;
                return fs::remove(fp);
            }

            /* --------------------------------------------------------------------------*/
            /**
            * @Synopsis  Rename the path.
            *
            * @Param frp
            * @Param top
            *
            * @Returns   
            */
            /* ----------------------------------------------------------------------------*/
            static bool rename(const std::string frp, std::string top) {
                fs::path fpfr(frp);
                fs::path fpto(top);

                if (!fs::exists(fpfr))
                    throw Exception("Source path not found...");
                if (fs::exists(fpto))
                    throw Exception("Target path already exists.");

                fs::rename(fpfr, fpto);

                return true;
            }
    };
}
#endif // FILEUTILS_H_
